/**
 * Copyright 2008 floop.pl
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package org.eaicompiler.util;

import org.eaicompiler.antlr.EventAIParser;
import org.eaicompiler.antlr.exceptions.InvalidArgumentException;
import org.eaicompiler.model.Defines;
import org.eaicompiler.model.Spawn;

/**
 * Various utility functions for parser
 * 
 * @author floop.pl
 */
public class Util {
	private static Defines	defines	= new Defines();

	public static void validatePercent(int v) {
		if (v < -100 || v > 100) {
			throw new InvalidArgumentException("Percentage value " + v + " is not between 0 and 100");
		}
	}

	public static int makeTime(int time, String suffix) {
		if ("s".equals(suffix)) {
			time = time * 1000;
		}
		return time;
	}

	public static String unQuoteString(String s) {
		String result = s;
		if (result.indexOf("\"") >= 0) {
			result = result.substring(s.indexOf('"') + 1, s.lastIndexOf('"'));
		}
		result = result.replaceAll("\\\\\"", "\"");
		return result;
	}

	public static Defines getDefines() {
		return defines;
	}

	public static int resolve(int definesType, String define) {
		define = Util.unQuoteString(define);
		return defines.resolve(definesType, define);
	}

	public static void define(int definesType, String define, int value) {
		define = Util.unQuoteString(define);
		defines.define(definesType, define, value);
	}

	public static String inverseResolve(int defineType, int value) {
		String result = defines.inverseResolve(defineType, value);
		if (result == null) {
			StringBuffer prefix = new StringBuffer();
			switch (defineType) {
				case Defines.DEFINE_CREATURE: {
					prefix.append("CREATURE_");
				}
				break;
				case Defines.DEFINE_EMOTE: {
					prefix.append("EMOTE_");
				}
				break;
				case Defines.DEFINE_UNIT_FIELD: {
					prefix.append("UNIT_FIELD_");
				}
				break;
				case Defines.DEFINE_FLAG: {
					prefix.append("FLAG_");
				}
				break;
				case Defines.DEFINE_QUEST: {
					prefix.append("QUEST_");
				}
				break;
				case Defines.DEFINE_SOUND: {
					prefix.append("SOUND_");
				}
				break;
				case Defines.DEFINE_SPELL: {
					prefix.append("SPELL_");
				}
				break;
				case Defines.DEFINE_INSTANCE_FIELD: {
					prefix.append("INSTANCE_FIELD_");
					break;
				}
				case Defines.DEFINE_TEMPLATE: {
					prefix.append("TEMPLATE_");
					break;
				}
			}
			prefix.append(value);
			result = prefix.toString();
			Util.getDefines().define(defineType, result, value);
		}
		result = Util.getDefines().inverseResolve(defineType, value);
		result = "\"" + result + "\"";
		return result;
	}

	public static Spawn spawn(EventAIParser.numberLiteral_return x, EventAIParser.numberLiteral_return y, EventAIParser.numberLiteral_return z,
			EventAIParser.numberLiteral_return o) {
		Spawn spawn = new Spawn();
		spawn.setX(parseNumber(x));
		spawn.setY(parseNumber(y));
		spawn.setZ(parseNumber(z));
		spawn.setO(parseNumber(o));
		return spawn;
	}

	public static float parseNumber(EventAIParser.numberLiteral_return v) {
		float r = Float.NaN;
		r = Float.parseFloat(v.getTree().toString());
		return r;
	}

	public static void cleanup() {
		defines = new Defines();
	}

	public static String quoteString(String string) {
		String result = string;
		result = result.replaceAll("\"", "\\\\\"");
		return result;
	}		
}
